package com.example.rabbitmqbootproducer.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.example.rabbitmqbootproducer.constant.MessageConstant.*;


/**
 * 直连模式配置
 *
 * @author 伯路平
 * @since 2023/6/19
 **/
@Configuration
public class DirectRabbitConfig {
    /**
     * 创建队列
     */
    @Bean
    public Queue getDirectQueueInstance() {
        return new Queue(DIRECT_QUEUE_NAME, true);
    }

    @Bean
    public Queue getDirectQueueInstance2() {
        return new Queue(DIRECT_QUEUE_NAME2, true);
    }

    @Bean
    public Queue getDirectExpiredQueueInstance(){
        return QueueBuilder.durable(EXPIRED_QUEUE_NAME).ttl(6000).build();
    }

    /**
     * 创建交换机
     */
    @Bean
    public DirectExchange getDirectExchangeInstance() {
        return new DirectExchange(DIRECT_EXCHANGE_NAME, true, false);
    }


    /**
     * 创建一个交换机，但是不给绑定队列，测试回调函数（结果：调用的的是confirmCallback,returnsCallback函数）
     */
    @Bean
    public DirectExchange getCallBackExchange() {
        return new DirectExchange(TEST_CALLBACK_EXCHANGE_NAME, true, false);
    }

    @Bean
    public DirectExchange getErrorBindingExchange() {
        return new DirectExchange(TEST_ERROR_BINDING_EXCHANGE_NAME, true, false);
    }

    /**
     * 创建绑定
     */
    @Bean
    public Binding getBindingInstance() {
        return BindingBuilder.bind(getDirectQueueInstance()).to(getDirectExchangeInstance()).with(DIRECT_ROUTING_KEY);
    }

    @Bean
    public Binding getBindingInstance2() {
        return BindingBuilder.bind(getDirectQueueInstance()).to(getErrorBindingExchange())
                .with(TEST_ERROR_BINDING_ROUTING_KEY);
    }

    @Bean
    public Binding getBindingInstance3() {
        return BindingBuilder.bind(getDirectExpiredQueueInstance()).to(getDirectExchangeInstance())
                .with(DIRECT_EXPIRED_ROUTING_KEY);
    }
}
